package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 目的地CURD
 * @author hc24
 *
 */
@Repository
public class CityInfoDao extends BaseDao {
	private final static Log logger=LogFactory.getLog(CityInfoDao.class);
	
	/**
	 * 根据条件分页查询城市信息
	 * @param dgm 分页参数
	 * @param cid 城市查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,CityInfoDomain cid){
		logger.debug("查询城市");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from cityinfo where 1=1");
		if(cid.getCityCn()!=null&&!"".equals(cid.getCityCn().trim())){
			sql.append(" and cityCn like ?");
			params.add("%"+cid.getCityCn()+"%");
		}
		if(cid.getCityType()!=-1){
			sql.append(" and cityType=?");
			params.add(cid.getCityType());
		}
		if(cid.getCountryId()!=null&&!"".equals(cid.getCountryId())){
			sql.append(" and countryId=?");
			params.add(cid.getCountryId());
		}
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))//判断是否排序
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, CityInfoDomain.class);
	}
	
	public int queryCountByCountry(String countryId){
		return super.queryTotalCount("select * from cityInfo where countryId=?", new Object[]{countryId});
	}
	
	/**
	 * 根据城市id获得城市和城市相关标配信息的价格
	 * @param cityIds 
	 * @return
	 */
	public List<Map<String, Object>> queryCityStdInfo(String[] cityIds){
		StringBuilder sql=new StringBuilder();
		sql.append("select t0.*,t1.stPrice as hotelPrice,t2.price as carPrice,t3.price as guidePrice from cityInfo t0");
		sql.append(" left join hotelInfo t1 on t0.hotelId=t1.hotelId");
		sql.append(" left join carInfo t2 on t0.carId=t2.carId");
		sql.append(" left join guideInfo t3 on t0.guideId=t3.guideId");
		sql.append(" where t0.cityId in(");
		for(int i=0;i<cityIds.length-1;i++){
			sql.append("?,");
		}
		sql.append("?)");
		return this.getJdbcTemplate().queryForList(sql.toString(), cityIds);
	}
	
	public CityInfoDomain getCityById(String cid){
		String sql="select * from cityInfo where cityId=?";
		List<CityInfoDomain> list=JdbcUtil.queryForList(this, CityInfoDomain.class, sql, cid);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	public CityInfoDomain getCityByCn(String cn){
		String sql="select * from cityInfo where cityCn=?";
		List<CityInfoDomain> list=JdbcUtil.queryForList(this, CityInfoDomain.class, sql, cn);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	public List<CityInfoDomain>  getLocationByCountryId(String countryId){
		String sql="select * from cityinfo where countryId =?";
		List<CityInfoDomain> list= JdbcUtil.queryForList(this, CityInfoDomain.class, sql, countryId);
		if(list.size()>0)
			return list;
		return null;
	}
	
	
	public void add(CityInfoDomain cityInfoDomain){
		super.addObj(cityInfoDomain);
	}
	
	public void update(CityInfoDomain cityInfoDomain){
		super.updateObj(cityInfoDomain, "cityId");
	}
	
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from cityInfo where cityid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
}
